//
//  RegisterVC.m
//  liBaQiang
//
//  Created by 奇林刘 on 2018/10/19.
//  Copyright © 2018年 liu. All rights reserved.
//

#import "RegisterVC.h"
#import "RegisterView.h"
#import "CheckVerificationPicView.h"

@interface RegisterVC ()

@property (nonatomic, strong) RegisterView *registerView;
@property (nonatomic, strong) CheckVerificationPicView *checkVeriPicView;
@property (nonatomic, strong) Liu70CountDownLabel *countDownLabel;

@end

@implementation RegisterVC

#pragma mark - Construction Script
- (void)configSelf {
    self.title = @"新用户注册";
}

- (void)allocateComponents {
    self.view.fp_addSubview(self.registerView = [[RegisterView alloc] init]);
    [self.registerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    self.view.fp_addSubview(self.countDownLabel = [[Liu70CountDownLabel alloc] init]);
    [self.countDownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.registerView.requestVerificationButton);
    }];
}

- (void)configComponents {
    self.registerView.registerButton.fp_addTargetActionForControlEvents(self, @selector(onRegisterButtonTap), UIControlEventTouchUpInside);
    self.registerView.requestVerificationButton.fp_addTargetActionForControlEvents(self, @selector(onRequestVerificationButtonTap), UIControlEventTouchUpInside);
    self.registerView.requestVerificationButton.fp_addTargetActionForControlEvents(self, @selector(onRequestVerificationButtonTap), UIControlEventTouchUpInside);
    self.countDownLabel.textColor = HexadecimalColor(@"2457B5");
}

#pragma mark - Events
- (void)onRegisterButtonTap {
    NSString *name = self.registerView.nameField.text;
    NSString *mobile = self.registerView.phoneField.text;
    NSString *password = self.registerView.passwordField.text;
    NSString *code = self.registerView.verificationField.text;
    
    if (name.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"姓名必填"];
        return;
    }
    if (password.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"密码必填"];
        return;
    }
    if (mobile.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"手机必填"];
        return;
    }
    if (code.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"验证码必填"];
        return;
    }
    [LBQRequestMobileLoginRegister goRequestWithName:name
                                              mobile:mobile
                                            password:password
                                                code:code
                                             success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                 [self.navigationController popViewControllerAnimated:YES];
                                             }
                                             failure:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                 
                                             }];
}

- (void)onRequestVerificationButtonTap {
    if (self.registerView.phoneField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请先填写手机号码！"];
        return;
    }
    
    [self.view addSubview:self.checkVeriPicView = [[CheckVerificationPicView alloc] init]];
    [self.checkVeriPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    self.checkVeriPicView.okButton.fp_addTargetActionForControlEvents(self, @selector(onPicVeriOKButtonTap), UIControlEventTouchUpInside);
}

- (void)onPicVeriOKButtonTap {
    [LBQRequestMobileLoginSendCode goRequestWithMobile:self.registerView.phoneField.text
                                              img_code:self.checkVeriPicView.textField.text
                                                  type:LBQRequestMobileLoginSendCodeTypeRegister
                                             verify_id:self.checkVeriPicView.veriID
                                               success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                   self.registerView.requestVerificationButton.hidden = YES;
                                                   [self.countDownLabel startCountDownToEndDate:[NSDate dateWithTimeIntervalSinceNow:60] finish:^{
                                                       self.registerView.requestVerificationButton.hidden = NO;
                                                   }];
                                               }
                                               failure:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                   
                                               }];
    [self.checkVeriPicView removeFromSuperview];
}

#pragma mark - Methods


@end
